<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use app\common\controller\Base;
use utils\Aes\Aes5;
use utils\Aes\Aes7;

/**
 * @title 前台用户中心
 */
class Member extends Base {

    private $member_info;

    public function _initialize() {
        parent::_initialize();

        // 权限
        $token = cookie('token');

        if (empty($token)) {
            $this->error('Cookie空，请重新登录', url('index/everyone/login'));
            exit;
        }


        $obj = new Aes7([
            'key' => APP_THEME
        ]);
        $member_arr = json_decode($obj->aesDe($token), true);


        // 以后优化，同过redis里查找token是否有键值，不要像现在这样每次都去请求数据库
        if (empty($member_arr)) {
            $this->error('请重新登录1', url('index/everyone/login'));
            exit;
        }


        $username = isset($member_arr['username']) ? $member_arr['username'] : '';
        $password = isset($member_arr['password']) ? $member_arr['password'] : '';
        $result = db('member')
                ->where('username', $username)
                ->where('password', $password)
                ->find();

        if (!$result) {
            $this->error('请重新登录2', url('index/everyone/login'));
            exit;
        }

        $this->member_info = $result;
        define('MID', $result['id']);
    }

    /**
     * @title 控制台
     */
    public function index() {

        // 总查询
        $count1 = db('num16_history')->where('num16_key', $this->member_info['username'])->count();

        // 正品数
        $count2 = db('num16_history')->where('num16_key', $this->member_info['username'])->where('num16_idd', '>', 0)->count();

        // 正品率
        if($count1 > 0){
            $count3 = $count2 / $count1;
        }else{
            $count3 = 0;
        }        
        $count3 = round($count3, 2);

        // 一周新增
        $count4 = db('num16_history')->where('num16_key', $this->member_info['username'])->where('create_time', '>', time() - 3600 * 24 * 7)->count();



        $this->assign('count1', $count1);
        $this->assign('count2', $count2);
        $this->assign('count3', $count3);
        $this->assign('count4', $count4);

        $this->assign('member_info', $this->member_info);



        return view();
    }

    /**
     * @title 查询历史
     */
    public function history_query() {


        $wheres = [
            'num16_key' => ['=', $this->member_info['username']]
        ];

        $lists = model('num16_history')->model_where($wheres)->order('id desc')->paginate(10, false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('pages', $lists->render());

        return view();
    }

    /**
     * @title 登录历史
     */
    public function history_login() {


        $wheres = [
            'mid' => ['=', MID]
        ];

        $lists = model('member_login_log')->model_where($wheres)->order('id desc')->paginate(10, false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('pages', $lists->render());


        return view();
    }

    /**
     * @title 密码修改
     */
    public function password() {

        if (request()->isPost()) {

            $password_old = input('post.password_old');
            $password = input('post.password');
            $password_again = input('post.password_again');


            if ($password != $password_again) {
                $this->error('两次输入的密码不一致');
            }

            //
            $check_old_pass = db('member')->where('id', MID)->where('password', my_md5($password_old))->count();

            if ($check_old_pass) {

                $affect_rows = db('member')->where('id', MID)->setField('password', my_md5($password));
                if ($affect_rows) {
                    $this->success('更新成功');
                } else {
                    $this->error('没有任何更新');
                }
            } else {
                $this->error('您输入的旧有密码不正确。');
            }
        } else {


            return view();
        }
    }

}
